#include "GWindow.h"
#include "GLog.h"
#include "GError.h"
#include "GMessageManager.h"

namespace GEngine
{
	const GWindowState GWindow::InitialState = {true, true, false, false, false, false, false};
	const GWindowStyle GWindow::DefaultStyle = {G_WT_CAPTIONED, {false, true, true, true}};

	GWindow::GWindow()
	{
		Handle = NULL;
		Title = "Window";

		Width = 0;
		Height = 0;
		PositionX = 0;
		PositionY = 0;

		VSynchEnabed = true;
		VSynchInterval = 1;

		State = InitialState;
		Style = DefaultStyle;

		MsgHandler = new GWindowMessageHandler();
		MsgHandler->SetTargetWindow(this);
		mGMessageManager->AppendHandler(MsgHandler);
	}

	GWindow::GWindow(GMessageHandler* Handler)
	{
		Handle = NULL;
		Title = "Window";

		Width = 0;
		Height = 0;
		PositionX = 0;
		PositionY = 0;

		VSynchEnabed = true;
		VSynchInterval = 1;

		State = InitialState;
		Style = DefaultStyle;

		MsgHandler = Handler;
	}

	GWindow::~GWindow()
	{
		mGMessageManager->RemoveHandler(MsgHandler);
		MsgHandler->SetTargetWindow(NULL);

		delete MsgHandler;
	}

	void* GWindow::GetHandle() const
	{
		return Handle;
	}

	const GWindowState& GWindow::GetState() const
	{
		return State;
	}

	const GWindowStyle&	GWindow::GetStyle() const
	{
		return Style;
	}

	const char* GWindow::GetTitle() const
	{
		return Title.c_str();
	}

	void GWindow::GetPosition(GInt32& X, GInt32& Y)
	{
		X = Width;
		Y = Height;
	}

	void GWindow::GetPosition(GMath::GVector2& Position)
	{
		Position.x = (float)PositionX;
		Position.y = (float)PositionY;
	}

	void GWindow::GetSize(GInt32& Width, GInt32& Height)
	{
		Width = this->Width;
		Height = this->Height;
	}

	void GWindow::GetSize(GMath::GVector2& Size)
	{
		Size.x = (float)Width;
		Size.y = (float)Height;
	}

	bool GWindow::GetFullScreen() const
	{
		return State.FullScreen;
	}

	bool GWindow::GetVSynch() const
	{
		return VSynchEnabed;
	}

	void GWindow::OnHide()
	{
		State.Visible = false;
	}

	void GWindow::OnShow()
	{
		State.Visible = true;
	}

	void GWindow::OnEnable()
	{
		State.Enabled = true;
	}

	void GWindow::OnDisable()
	{
		State.Enabled = false;
	}

	void GWindow::OnFocusGain()
	{
		State.Focused = true;
	}

	void GWindow::OnFocusLoose()
	{
		State.Focused = false;
	}

	void GWindow::OnRestore()
	{
		State.Minimized = false;
		State.Maximized = false;
	}

	void GWindow::OnMinimize()
	{
		State.Minimized = true;
		State.Maximized = false;
	}

	void GWindow::OnMaximize()
	{
		State.Minimized = false;
		State.Maximized = true;
	}

	void GWindow::OnWindowed()
	{
		State.FullScreen = false;
	}

	void GWindow::OnFullScreen()
	{
		State.FullScreen = true;
	}

	void GWindow::OnMove(GInt32 PosX, GInt32 PosY)
	{
		PositionX = PosX;
		PositionY = PosY;
	}

	void GWindow::OnSize(GInt32 Width, GInt32 Height)
	{
		this->Width = Width;
		this->Height = Height;
	}

	void GWindow::OnSizing()
	{
	}

	void GWindow::OnSizeEnd()
	{
		State.Modal = false;
	}

	void GWindow::OnSizeStart()
	{
		State.Modal = true;
	}

}